package com.hr.剑指offer.work2;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author ：zhz
 * @date ：Created in 2020/11/30
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @description:
 **/
public class FindRepeatNumber_3 {
    /**
     * 数组中重复的数字 1
     */
    public static int findRepeatNumber(int[] nums) {
        int res = 0;
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            if (!set.contains(num)) {
                set.add(num);
            } else {
                res = num;
            }
        }
       return res;
    }
    /**
     * 数组中重复的数字 2
     */
    public static int findRepeatNumber2(int[] nums) {
        int res = 0;
        Arrays.sort(nums);
        for (int i=0;i<nums.length-2;i++) {
           if (nums[i]==nums[i+1]){
               res=nums[i];
           }
        }
       return res;
    }

    public static void main(String[] args) {
        int[] nums={1,1,2,5,4,7,8};
        System.out.println(FindRepeatNumber_3.findRepeatNumber(nums));
        System.out.println(FindRepeatNumber_3.findRepeatNumber2(nums));
    }
}
